#!/usr/bin/env/python
import web, time
import flickr

urls = (
	'/', 'index',
	'/pylickrsearch', 'pylickrsearch',
	'/pylickrimage/(.*)', 'pylickrimage',
	'/pylickrtag/(.*)', 'pylickrtag',
	'/images/(.*)', 'images',
	'/scripts/(.*)', 'scripts',
	'/favicon.ico', 'favicon',
)

		
class index:
	def GET(self):
		input  = web.input()
		
		if input.has_key("term"):
			(found,images) = flickr.search(input.term,int(input.page))
			pages = int(found.replace(",",""))/24
			image_result = ""
			for img in images:
				image_result += """<img src="%s" onclick="javascript: loadImage(this.alt);" alt="%s" title="%s"/>"""%(img[0][:-6]+"_s.jpg",img[0][:-6]+".jpg",img[1])
			result = """We found %s images... thats about %s pages!<br/>
			<div id="gallery" style="width: 450px; float: left;">%s</div>
			<img style="position: fixed; right: 0px;" alt="click on an icon to load me" id="viewer"/> 
			<p id="status"></p>
			"""%(found,pages,image_result)
		else:
			result = ""
		print open("templates/index.html", "r").read().replace("@result@", result)

class pylickrsearch:
	def GET(self):
		input  = web.input()
		
		if input.has_key("term"):
			(found,images) = flickr.search(input.term,int(input.page))
			try:
				pages = int(found.replace(",",""))/24
				image_result = ""
				for img in images:
					image_result += """<img src="%s" onmouseover="mouseOver(this)" onmouseout="mouseOut(this)" onclick="javascript: loadImage(this);" alt="%s" align="%s" title="%s"/>"""%(img[0][:-6]+"_s.jpg",img[0][:-6]+".jpg",img[2],img[1])
				image_result +="""
				<button onmouseover="mouseOver(this)" onmouseout="mouseOut(this)" onclick="javascript: document.getElementById('page').value=%s; doSearch();">Prev</button>
				<button onmouseover="mouseOver(this)" onmouseout="mouseOut(this)" onclick="javascript: document.getElementById('page').value=%s; doSearch();">Next</button>
				"""%(int(input.page)-1,int(input.page)+1)
			except:
				image_result = "No more matches"
		print image_result
		
class pylickrimage:
	def GET(self,name):
		input  = web.input()
		web.header("Content-Type", "image/jpeg")
		print flickr.image(name)
		
class pylickrtag:
	def GET(self,name):
		web.header("Content-Type", "text/html")
		input  = web.input()
		terms = input.terms.split(" ")

		print '<b>Alternative Tags</b><ul id="tags">'
		for tag in flickr.tag(name):
			printTag = True
			for term in terms:
				if term == tag:
					printTag = False
			if printTag:
				print """
						<li onmouseover="mouseOver(this)" onmouseout="mouseOut(this)">
							<img onclick="javascript: document.getElementById(\'term\').value+=\' %s\'; resetPage(); doSearch();" style="float: left" src="/images/plus.gif" align="top" title="Add '%s' to search results" />
							<div style="text-align: center; float: left; width: 268px;" onclick="javascript: document.getElementById(\'term\').value=\'%s\'; resetPage(); doSearch();">%s</div>
							<img onclick="javascript: document.getElementById(\'term\').value+=\' -%s\'; resetPage(); doSearch();" style="float: right;" src="/images/x.gif" align="top" title="Remove '%s' from search results" />
						</li>
						"""%(tag,tag,tag,tag,tag,tag)
		print "</ul><br/>"
		
class images:
	def GET(self,name):
		ext = name.split(".")[-1]
		
		cType = {"png":"images/png","jpg":"image/jpeg","gif":"image/gif"}
		
		web.header("Content-Type", cType[ext])
		print open('images/%s'%name,"rb").read()
		
class scripts:
	def GET(self,name):
		ext = name.split(".")[-1]
		
		cType = {"js":"text/javascript","css":"text/css","gif":"image/gif",
				"png":"images/png","jpg":"image/jpeg",}
		
		web.header("Content-Type", cType[ext])
		if cType[ext].find("image") > -1:
			print open('scripts/%s'%name,"rb").read()
		else:
			print open('scripts/%s'%name,"r").read()
		
class favicon:
	def GET(self):
		web.header("Content-Type", "image/x-icon")
		print open('favicon.ico',"rb").read()

web.webapi.internalerror = web.debugerror

if __name__ == "__main__": web.run(urls, globals(), web.reloader)